﻿$(function(){
	
	$(".layout").each(function(){
		var height=$(this).attr("height")!=null?$(this).attr("height"):'100%';//整个布局所总宽度
		var heightDiff=$(this).attr("heightDiff")!=null?$(this).attr("heightDiff"):0;//高度补差
		var width=$(this).attr("leftWidth")!=null?$(this).attr("leftWidth"):200;
		var topHeight=$(this).attr("topHeight")!=null?$(this).attr("topHeight"):0;
		var bottomHeight=$(this).attr("bottomHeight")!=null?$(this).attr("bottomHeight"):0;
		var allowLeftResize=$(this).attr("allowLeftResize")!=null?$(this).attr("allowLeftResize"):true;
		var allowLeftCollapse=$(this).attr("allowLeftCollapse")!=null?$(this).attr("allowLeftCollapse"):true;
		var isLeftCollapse=$(this).attr("isLeftCollapse")!=null?$(this).attr("isLeftCollapse"):false;
		var config={};
		config.space=0;
		config.leftWidth=new Number(width);//左侧宽度
		config.topHeight=new Number(topHeight);//上部宽度
		config.bottomHeight=new Number(bottomHeight);//下部宽度
		config.allowLeftResize=allowLeftResize;//是否允许 左边可以调整大小
		config.allowLeftCollapse=allowLeftCollapse;//是否允许 左边可以隐藏
		config.isLeftCollapse=isLeftCollapse;//初始化时是否隐藏
		config.height=height;
		config.heightDiff=new Number(heightDiff);
		if(navigator.userAgent.indexOf("MSIE")>0) {    
            config.heightDiff=new Number(0);        //IE浏览器无需补差  
		}  
		$(this).ligerLayout(config);
	});

	$(".uigrid").each(function(){
		var url=$(this).attr("url");
		var checkbox=$(this).attr("checkbox")=="true"?true:false;
		var autoload=$(this).attr("autoload")=="false"?false:true;
		var page=$(this).attr("page")=="false"?false:true;
		var title=$(this).attr("title")!=null?$(this).attr("title"):null;
		var width=$(this).attr("width")!=null?$(this).attr("width"):'auto';
		var pageSizeOptions = [];
		if($(this).attr("pageSizeOptions")!=null){
			var pageSizeOptionsStr = $(this).attr("pageSizeOptions");
			pageSizeOptions = pageSizeOptionsStr.split(/\s*\[\s*|\s*,\s*|\s*\]\s*/);
		}
		else{
			pageSizeOptions = [20,40,60,600];
		}
//		var height='96%';
		
		var queryHeight = 0;
		$(".searchDiv").each(function(){
			queryHeight = queryHeight + $(this).outerHeight(true);
		});
		var height=$(window).height()-$(".toolbar").outerHeight(true)-queryHeight-27; //27为l-tab-links导航的高度
		if($(".l-layout-center div").hasClass("l-layout-header")){
			height = height - 25;
		}
		
		if($(this).attr("height")!=null){
			height=$(this).attr("height");
		}else {
			if(navigator.userAgent.indexOf("MSIE")>0) {    
	             height='100%';       //IE浏览器无需补差  
			}  
		}	
		var heightDiff=$(this).attr("heightDiff")!=null?parseFloat($(this).attr("heightDiff")):0;
		var enabledEdit=$(this).attr("enabledEdit")=="true"?true:false;
		var dataAction=$(this).attr("dataAction")!=null?$(this).attr("dataAction"):"server";
		var selectRowButtonOnly=$(this).attr("selectRowButtonOnly")!=null?$(this).attr("selectRowButtonOnly"):false;
		var pagesize=$(this).attr("pagesize")!=null?$(this).attr("pagesize"):20;
		var pk=$(this).attr("pk");
		var onSelectRow=$(this).attr("onSelectRow")!=null?$(this).attr("onSelectRow"):null;
		
		var onCheckRow=$(this).attr("onCheckRow")!=null?$(this).attr("onCheckRow"):null;
		var onDblClickRow = $(this).attr("onDblClickRow")!=null?$(this).attr("onDblClickRow"):null;
		//var rowAttrRender = $(this).attr("rowAttrRender")!=null?$(this).attr("rowAttrRender"):null;
		//详细
		var detail = $(this).attr("detail")!=null?$(this).attr("detail"):null;
		var onCheckAllRow = $(this).attr("onCheckAllRow")!=null?$(this).attr("onCheckAllRow"):null;
		var onBeforeCheckAllRow = $(this).attr("onBeforeCheckAllRow")!=null?$(this).attr("onBeforeCheckAllRow"):null;
		var onAfterShowData = $(this).attr("onAfterShowData")!=null?$(this).attr("onAfterShowData"):null;
		
		var dateFormat = $(this).attr("dateFormat")!=null?$(this).attr("dateFormat"):'yyyy-MM-dd';
		//获取查询参数的值
		var queryForm = $(this).attr("queryForm");
		var queryParms = $("#"+queryForm).formToArray();
		//是否允许调整列宽
		var allowAdjustColWidth =$(this).attr("allowAdjustColWidth")=="false"?false:true;
		
		var columns="[";
		$(this).find("span").each(function(){	
			var isHide=$(this).attr("isHide")!=null?$(this).attr("isHide"):false;
			var align=$(this).attr("align")!=null?$(this).attr("align"):"center";
			var renderFun=$(this).attr("render");
			var datatype=$(this).attr("datatype")!=null?$(this).attr("datatype"):"string";
			var editor = $(this).attr("editor")!=null?$(this).attr("editor"):null; //可编辑grid
			var sort = $(this).attr("sort")!=null?$(this).attr("sort"):"false"; //sort属性	
			//国定列
			var frozen = $(this).attr("frozen")=="true"?true:false;
			
			columns+="{";
			if(renderFun){
				columns+="render:"+renderFun+",";
			}
			var width=$(this).attr("width");
			if(width!=null){
			 columns+="width:"+width+","; 
			}
			if(editor!=null) {
				columns+="editor:"+editor+",";
			}
			columns+="hide:"+isHide+",";
			columns+="align:\""+align+"\",";
			columns+="type:'"+datatype+"',";
			columns+="frozen:"+frozen+",";
			columns+="name:\""+$(this).attr("name")+"\",";
			columns+="isSort:\""+sort+"\",";
			columns+="display:\""+$(this).html()+"\""; 
			columns+="},";
		});
		//columns+=renderOption;
		columns=columns.substring(0,columns.length-1);
		columns+="]";
		var columnsObj=eval("("+columns+")");
		if(height=="100%"){
			heightDiff=-5;
		}
		//columnsObj.push(renderOption);
		$(this).ligerGrid({
	            	url:url,
	            	title:title,
	            	heightDiff:heightDiff,
	            	checkbox:checkbox,
	            	autoload:autoload,
	            	dataAction: dataAction,
	                columns:columnsObj,
	                usePager:page,
	                pageSize:pagesize,
	                width: width,height:height,
	                pageSizeOptions: pageSizeOptions,
	                allowHideColumn:false,
	                root:"rows",record:"total",pageParmName :"page",pagesizeParmName:"pagesize",
	                switchPageSizeApplyComboBox:false,
	                enabledEdit:enabledEdit,
	                selectRowButtonOnly:selectRowButtonOnly,
	                onSelectRow: eval(""+onSelectRow+""),
	                onCheckRow: eval(""+onCheckRow+""),
	                onDblClickRow:eval(""+onDblClickRow+""),
	                //rowAttrRender:eval(""+rowAttrRender+""),
	                onCheckAllRow:eval(""+onCheckAllRow+""),
	                onBeforeCheckAllRow:eval(""+onBeforeCheckAllRow+""),
	                onAfterShowData:eval(""+onAfterShowData+""),
//	                detail:eval(""+detail+""),
	                detail:eval("("+detail+")"),
	                dateFormat:dateFormat,
	                onError:ajaxError,
	                parms:queryParms,
	                allowAdjustColWidth:allowAdjustColWidth
			});

	});	

	/**
	 修改表单输入框样式
	**/
	/* 
	$("form").each(function(){
		var texts = $(this).find("input[type='text'],input[type='password']");
		texts.each(function(){
			var ltype = $(this).attr("ltype");
			if(ltype==null){
			    var type = $(this).attr("type");
				$(this).attr("ltype",type);
			}
		});
	 	
		var selects = $(this).find("select");
		selects.each(function(){
			var ltype = $(this).attr("ltype");
			if(ltype==null){
				$(this).attr("ltype","select");
			}
		});
		
	});
	 
	//执行修饰
	 $("form").ligerForm();
	 **/
	
	$(".listTable tr").hover(function() {
	    	$(this).css("background-color","#FFFFAA");
	  	},function(){
	  	     $(this).css('background-color','#FFFFFF');  
	 }); 
});

var grid={
	reload:function(id){
		var manager = $("#"+id).ligerGetGridManager(); 
		manager.loadData(true);
		if($(".l-grid-hd-cell-inner").closest("tr").hasClass("l-checked")){
			$(".l-grid-hd-cell-inner").closest("tr").removeClass("l-checked");
		}
	},
	getSelectRowDatas:function(gridId,columnId){
		var manager = $("#"+gridId).ligerGetGridManager(); 
		var rows = manager.getCheckedRows();
		if(rows.length==0 &&manager.getSelectedRow()!=null){
			rows.push(manager.getSelectedRow());
		}
		var ids = [];
		var pk=columnId;
		if(pk==null){
			return null;
		}
		$(rows).each(function (){
			ids.push(this[pk]);
		});
		if(ids.length<1){
			return null;
		}
		return ids;
	},
	getSelectRowIds:function(id){
		var manager = $("#"+id).ligerGetGridManager(); 
		var rows = manager.getCheckedRows();
		if(rows.length==0 &&manager.getSelectedRow()!=null){
			rows.push(manager.getSelectedRow());
		}
		var ids = [];
		var pk=$("#"+id).attr("pk");
		if(pk==null){
			return null;
		}
		$(rows).each(function (){
			ids.push(this[pk]);
		});
		if(ids.length<1){
			return null;
		}
		return ids;
	},	
	getRows:function(id){
		var manager = $("#"+id).ligerGetGridManager(); 
		var rows = manager.getCheckedRows();
		if(rows.length==0 &&manager.getSelectedRow()!=null){
			rows.push(manager.getSelectedRow());
		}
		return rows;
	},
	deleteRows:function(id){
		var manager = $("#"+id).ligerGetGridManager(); 
		var rows = manager.getCheckedRows();
		if(rows.length==0 &&manager.getSelectedRow()!=null){
			rows.push(manager.getSelectedRow());
		}
		var ids = [];
		var pk=$("#"+id).attr("pk");
		if(pk==null){
			return;
		}
		$(rows).each(function (){
			ids.push(this[pk]);
		});
		if(ids.length<1){
			return;
		}
		var idstr = ids.join(',');		
		var url=$("#"+id).attr("url");
		var delete_url = $("#"+id).attr("delete-url");
		var callback=$("#"+id).attr("delete-callback");		
		$.ajax({type: "POST",
			url: delete_url,
			dataType: "json",
			data: "ids="+idstr,
			success: function(rst){ 
				eval(callback+"("+rst.success+",'"+rst.msg+"');");
				if(rst.success)	manager.loadData();
				}
			});
	},
	search:function(gridId,formId){
		$("#"+formId).submit( function () {
			  return false;
		});
		var manager = $("#"+gridId).ligerGetGridManager(); 
		var parms=$("#"+formId).serializeArray();
		manager.setOptions({parms: parms,newPage:1});
		manager.changePage('first');
		manager.loadData();
		if($(".l-grid-hd-cell-inner").closest("tr").hasClass("l-checked")){
			$(".l-grid-hd-cell-inner").closest("tr").removeClass("l-checked");
		}
	} 
}

 
